/*************************************************************
 * File: ResourceSupport.java
 * Copyright (c) 2012
 * Author: Shaobin.Software@gmail.com Shaobin_Software@163.com 
 * Date: Jan 10, 2013
 * Description: 
 ************************************************************/

package snow.core.web.restlet;

import java.util.Locale;

import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.commons.lang.ArrayUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.restlet.Request;
import org.restlet.Response;
import org.restlet.data.Form;
import org.restlet.data.Reference;
import org.restlet.ext.servlet.ServletUtils;
import org.restlet.representation.EmptyRepresentation;
import org.restlet.representation.Representation;
import org.springframework.context.ApplicationContext;
import org.springframework.web.context.support.WebApplicationContextUtils;

import snow.core.web.Result;

/**
 * 系统中所有resource类的基类
 * 
 * @author Shaobin.Software@gmail.com Shaobin_Software@163.com
 * @version 1.0
 */
public abstract class ResourceSupport {

	protected final Log log = LogFactory.getLog(getClass());

	protected Representation getCurrentEntity() {
		Request request = Request.getCurrent();
		log.debug("当前Request对象为 -> " + request);
		Representation entity = request.getEntity();
		log.debug("当前的Representation对象为 -> " + entity);
		return entity;
	}

	protected Locale getCurrentLocale() {
		Locale locale = getRequest().getLocale();
		return locale == null ? Locale.getDefault() : locale;
	}

	protected Form getPostAsForm() {
		Representation entity = getCurrentEntity();
		if (entity instanceof EmptyRepresentation) {
			return new Form();
		}
		return new Form(entity);
	}

	protected Form getQueryAsForm() {
		Request request = Request.getCurrent();
		log.debug("当前Request对象为 -> " + request);
		Reference reference = request.getResourceRef();
		log.debug("当前Reference对象为 -> " + reference);
		return reference.getQueryAsForm();
	}

	protected HttpServletRequest getRequest() {
		return ServletUtils.getRequest(Request.getCurrent());
	}

	protected HttpServletResponse getResponse() {
		return ServletUtils.getResponse(Response.getCurrent());
	}

	protected Result getResultFrom(String code, Object message) {
		return new Result(code, message) {

			/**
			 * 
			 */
			private static final long serialVersionUID = 5701085684373095403L;

			public Object getMessage() {
				Object message = super.getMessage();
				if (message instanceof String) {
					HttpSession session = getSession();
					ServletContext sc = session.getServletContext();
					ApplicationContext context = null;
					context = WebApplicationContextUtils.getWebApplicationContext(sc);
					if (context != null) {
						return context.getMessage((String) message, ArrayUtils.EMPTY_OBJECT_ARRAY, getCurrentLocale());
					}
				}
				return message;
			}
		};
	}

	protected ServletContext getServletContext() {
		return getSession().getServletContext();
	}

	protected HttpSession getSession() {
		return getRequest().getSession();
	}

	protected Result getSuccessResult() {
		return getSuccessResultFrom("success.operation");
	}

	protected Result getSuccessResultFrom(Object message) {
		return getResultFrom("000000", message);
	}
}
